package com.bzyd.ss.handler;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 403无权限报错处理器
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        // 设置响应状态
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        // 设置响应类型
        response.setContentType("text/html;charset=UTF-8");
        //响应输出
        response.getWriter()
                .println("<html><body><div style='width:800px;text-align:center;margin:auto;font-size:24px;'>无权限，请联系管理员</div></body></html>");
        // 刷新输出流缓存
        response.getWriter().flush();
    }
}
